﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CentralVacuum.Common
{
    public class Recipe
    {
        /// <summary>
        /// 配方id
        /// </summary>
        public string recipeID { get; set; }   // 唯一id
        /// <summary>
        /// eps品牌
        /// </summary>
        public string epsName { get; set; }  // eps品牌
        /// <summary>
        /// 设定密度
        /// </summary>
        public string setDensity { get; set; }   // 设定密度
        /// <summary>
        /// 设定重量
        /// </summary>
        public string setWeight { get; set; }    // 设定重量
        /// <summary>
        /// 设定压力
        /// </summary>
        public string setPress { get; set; } // 设定压力
        /// <summary>
        /// 设定温度
        /// </summary>
        public string setTemp { get; set; }  // 设定温度
        /// <summary>
        /// 排气比例阀开度
        /// </summary>
        public string setExhaust { get; set; }   // 排气比例阀开度
        /// <summary>
        /// 最大加热时间
        /// </summary>
        public string setMaxHeatTime { get; set; }   // 最大加热时间
        /// <summary>
        /// 最小加热时间
        /// </summary>
        public string setMinHeatTime { get; set; }   // 最小加热时间
        /// <summary>
        /// 料位传感器选择(=0传感器,=1时间)
        /// </summary>
        public int setSensor { get; set; }   // 料位传感器选择(=0传感器,=1时间)
        /// <summary>
        /// 设置加热模式(=0恒压,=1恒温)
        /// </summary>
        public int setHeatingMode { get; set; }  // 设置加热模式(=0恒压,=1恒温)
        /// <summary>
        /// 旁通小排气小阀
        /// </summary>
        public int setSmallValve { get; set; }   // 旁通小排气小阀
        /// <summary>
        /// 旁通小排气大阀
        /// </summary>
        public int setBigValve { get; set; } // 旁通小排气大阀
        /// <summary>
        /// 设备pn号
        /// </summary>
        public string pn { get; set; }   // 设备pn号

        public Recipe()
        {
            recipeID = "XD303-18.5";
            epsName = "XD303";
            setDensity = "18.5";
            setWeight = "22.5";
            setPress = "0.25";
            setTemp = "100.5";
            setExhaust = "65";
            setMaxHeatTime = "80.0";
            setMinHeatTime = "20.0";
            setSensor = 0;
            setHeatingMode = 0;
            setSmallValve = 1;
            setBigValve = 0;
            pn = "";
        }

        /// <summary>
        /// 配方参数
        /// </summary>
        /// <param name="recipeID">配方id</param>
        /// <param name="epsName">eps品牌</param>
        /// <param name="setDensity">设定密度</param>
        /// <param name="setWeight">设定重量</param>
        /// <param name="setPress">设定压力</param>
        /// <param name="setTemp">设定温度</param>
        /// <param name="setExhaust">设定排气比例阀</param>
        /// <param name="setMaxHeatTime">最大加热时间</param>
        /// <param name="setMinHeatTime">最小加热时间</param>
        /// <param name="setSensor">传感器选择</param>
        /// <param name="setHeatingMode">加热模式</param>
        /// <param name="setSmallValve">旁通排气小阀</param>
        /// <param name="setBigValve">旁通排气大阀</param>
        public Recipe(string recipeID, string epsName, string setDensity, string setWeight, string setPress, string setTemp, string setExhaust, string setMaxHeatTime, string setMinHeatTime, int setSensor, int setHeatingMode, int setSmallValve, int setBigValve, string pn)
        {
            this.recipeID = recipeID;
            this.epsName = epsName;
            this.setDensity = setDensity;
            this.setWeight = setWeight;
            this.setPress = setPress;
            this.setTemp = setTemp;
            this.setExhaust = setExhaust;
            this.setMaxHeatTime = setMaxHeatTime;
            this.setMinHeatTime = setMinHeatTime;
            this.setSensor = setSensor;
            this.setHeatingMode = setHeatingMode;
            this.setSmallValve = setSmallValve;
            this.setBigValve = setBigValve;
            this.pn = pn;
        }
    }

    class RecipeXML
    {
        /// <summary>
        /// 创建配方XML文件
        /// </summary>
        /// <param name="xmlFilePath">存储路径</param>
        /// <param name="recipeList">配方列表</param>
        public static void GenerateRecipeXMLFile(string xmlFilePath, List<Recipe> recipeList)
        {
            try
            {
                //初始化一个xml实例
                XmlDocument myXmlDoc = new XmlDocument();
                //创建xml的根节点
                XmlElement rootElement = myXmlDoc.CreateElement("Recipes");
                //将根节点加入到xml文件中（AppendChild）
                myXmlDoc.AppendChild(rootElement);

                foreach (var item in recipeList)
                {
                    //初始化第一层的第一个子节点
                    XmlElement firstLevelElement1 = myXmlDoc.CreateElement("Recipe");
                    //填充第一层的第一个子节点的属性值（SetAttribute）
                    firstLevelElement1.SetAttribute("ID", item.recipeID);
                    //将第一层的第一个子节点加入到根节点下
                    rootElement.AppendChild(firstLevelElement1);
                    //初始化第二层的第一个子节点
                    XmlElement secondLevelElement11 = myXmlDoc.CreateElement("epsName");
                    //填充第二层的第一个子节点的值（InnerText）
                    secondLevelElement11.InnerText = item.epsName.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement11);
                    XmlElement secondLevelElement12 = myXmlDoc.CreateElement("setDensity");
                    secondLevelElement12.InnerText = item.setDensity.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement12);
                    XmlElement secondLevelElement13 = myXmlDoc.CreateElement("setWeight");
                    secondLevelElement13.InnerText = item.setWeight.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement13);
                    XmlElement secondLevelElement14 = myXmlDoc.CreateElement("setPress");
                    secondLevelElement14.InnerText = item.setPress.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement14);
                    XmlElement secondLevelElement15 = myXmlDoc.CreateElement("setTemp");
                    secondLevelElement15.InnerText = item.setTemp.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement15);
                    XmlElement secondLevelElement16 = myXmlDoc.CreateElement("setExhaust");
                    secondLevelElement16.InnerText = item.setExhaust.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement16);
                    XmlElement secondLevelElement17 = myXmlDoc.CreateElement("setMaxHeatTime");
                    secondLevelElement17.InnerText = item.setMaxHeatTime.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement17);
                    XmlElement secondLevelElement18 = myXmlDoc.CreateElement("setMinHeatTime");
                    secondLevelElement18.InnerText = item.setMinHeatTime.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement18);
                    XmlElement secondLevelElement19 = myXmlDoc.CreateElement("setSensor");
                    secondLevelElement19.InnerText = item.setSensor.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement19);
                    XmlElement secondLevelElement1_10 = myXmlDoc.CreateElement("setHeatingMode");
                    secondLevelElement1_10.InnerText = item.setHeatingMode.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement1_10);
                    XmlElement secondLevelElement1_11 = myXmlDoc.CreateElement("setSmallValve");
                    secondLevelElement1_11.InnerText = item.setSmallValve.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement1_11);
                    XmlElement secondLevelElement1_12 = myXmlDoc.CreateElement("setBigValve");
                    secondLevelElement1_12.InnerText = item.setBigValve.ToString();
                    firstLevelElement1.AppendChild(secondLevelElement1_12);
                }
                //将xml文件保存到指定的路径下
                myXmlDoc.Save(xmlFilePath);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        /// <summary>
        /// 读取配方XML文件
        /// </summary>
        /// <param name="xmlFilePath">存储路径</param>
        /// <param name="recipeList">配方列表</param>
        public static void ReadRecipeXMLFile(string xmlFilePath, out List<Recipe> recipeList)
        {
            recipeList = new List<Recipe>();

            //初始化一个xml实例
            XmlDocument myXmlDoc = new XmlDocument();
            //加载xml文件（参数为xml文件的路径）
            myXmlDoc.Load(xmlFilePath);
            //获得第一个匹配的节点（SelectSingleNode）：此xml文件的根节点
            XmlNode rootNode = myXmlDoc.SelectSingleNode("Recipes");
            //分别获得该节点的InnerXml和OuterXml信息
            //string innerXmlInfo = rootNode.InnerXml.ToString();
            //string outerXmlInfo = rootNode.OuterXml.ToString();
            //获得该节点的子节点（即：该节点的第一层子节点）
            XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
            try
            {
                foreach (XmlNode node in firstLevelNodeList)
                {
                    Recipe recipe = new Recipe();
                    //获得该节点的属性集合
                    XmlAttributeCollection attributeCol = node.Attributes;
                    foreach (XmlAttribute attri in attributeCol)
                    {
                        //获取属性名称与属性值
                        string name = attri.Name;
                        string value = attri.Value;
                        recipe.recipeID = value;//相机ID
                    }

                    //判断此节点是否还有子节点
                    if (node.HasChildNodes)
                    {
                        //获取该节点的第一个子节点
                        XmlNode secondLevelNode1 = node.FirstChild;
                        //获取该节点的名字
                        string name = secondLevelNode1.Name;
                        //获取该节点的值（即：InnerText）
                        string innerText = secondLevelNode1.InnerText;
                        recipe.epsName = innerText;//EPS品牌

                        //获取该节点的第二个子节点（用数组下标获取）
                        XmlNode secondLevelNode2 = node.ChildNodes[1];
                        name = secondLevelNode2.Name;
                        innerText = secondLevelNode2.InnerText;
                        recipe.setDensity = innerText;//设定密度
                        XmlNode secondLevelNode3 = node.ChildNodes[2];
                        name = secondLevelNode3.Name;
                        innerText = secondLevelNode3.InnerText;
                        recipe.setWeight = innerText;//设定重量
                        XmlNode secondLevelNode4 = node.ChildNodes[3];
                        name = secondLevelNode4.Name;
                        innerText = secondLevelNode4.InnerText;
                        recipe.setPress = innerText;//设定压力
                        XmlNode secondLevelNode5 = node.ChildNodes[4];
                        name = secondLevelNode5.Name;
                        innerText = secondLevelNode5.InnerText;
                        recipe.setTemp = innerText;//设定温度
                        XmlNode secondLevelNode6 = node.ChildNodes[5];
                        name = secondLevelNode6.Name;
                        innerText = secondLevelNode6.InnerText;
                        recipe.setExhaust = innerText;//设定排气比例阀
                        XmlNode secondLevelNode7 = node.ChildNodes[6];
                        name = secondLevelNode7.Name;
                        innerText = secondLevelNode7.InnerText;
                        recipe.setMaxHeatTime = innerText;//设定最大加热时间
                        XmlNode secondLevelNode8 = node.ChildNodes[7];
                        name = secondLevelNode8.Name;
                        innerText = secondLevelNode8.InnerText;
                        recipe.setMinHeatTime = innerText;//设定最小加热时间
                        XmlNode secondLevelNode9 = node.ChildNodes[8];
                        name = secondLevelNode9.Name;
                        innerText = secondLevelNode9.InnerText;
                        recipe.setSensor = Convert.ToInt16(innerText);//传感器选择
                        XmlNode secondLevelNode10 = node.ChildNodes[9];
                        name = secondLevelNode10.Name;
                        innerText = secondLevelNode10.InnerText;
                        recipe.setHeatingMode = Convert.ToInt16(innerText);//加热模式
                        XmlNode secondLevelNode11 = node.ChildNodes[10];
                        name = secondLevelNode11.Name;
                        innerText = secondLevelNode11.InnerText;
                        recipe.setSmallValve = Convert.ToInt16(innerText);//旁通排气小阀
                        XmlNode secondLevelNode12 = node.ChildNodes[11];
                        name = secondLevelNode12.Name;
                        innerText = secondLevelNode12.InnerText;
                        recipe.setBigValve = Convert.ToInt16(innerText);//旁通排气大阀
                    }
                    recipeList.Add(recipe);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

